﻿using DAO.BLL;
using NHibernate.Criterion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.helper;
using Common;
using log4net;
using System.Reflection;
using CZLogger;
using Domain;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
namespace Web.Controllers
{
    public class TempletController : BaseController
    {
        //单元测试
        //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();


        #region ActionResult
        public ActionResult Index()
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "user")]
        public new ActionResult User(int pageIndex = 1, int pageSize = 10)
        {
            B_Users b_user = new B_Users();
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="",value=new int[]{pageIndex,pageSize},searchType=Common.EnumBase.SearchType.Paging}
            };
            var list_user = b_user.GetList(st,null);
            return View();
        }

        //[AuthorizeFilter(ActionName = "authority")]
        public ActionResult Authority()
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "navigation")]
        public ActionResult Navigation()
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "manager_role")]
        public ActionResult Manager_role() 
        {
            //获取所有的后台导航
            B_Navigation b_nav = new B_Navigation();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };

            List<Domain.Navigation> list = new List<Domain.Navigation>();
            var list_nav = b_nav.LoadAll(order, 0);
            foreach (var nav in list_nav)
            {
                list.Add(nav);
                //查询二级菜单
                var list_sub_nav = b_nav.LoadAll(order, nav.id);
                foreach (var sub_nav in list_sub_nav)
                {
                    list.Add(sub_nav);
                }
            }
            ViewBag.list_nav = list;
            return View();
        }

        [AuthorizeFilter(ActionName = "manager")]
        public ActionResult Manager() 
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "manager_log")]
        public ActionResult Manager_log() 
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "send_sms")]
        public ActionResult Send_sms() 
        {
            return View();
        }

        [AuthorizeFilter(ActionName = "backups")]
        public ActionResult Backups() 
        {
            B_Backups b_backup = new B_Backups();
            string dbName = b_backup.GetDbName();
            ViewData["dbName"] = dbName;
            return View();
        }

        [AuthorizeFilter(ActionName = "news_type")]
        public ActionResult News_type() 
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "news")]
        public ActionResult News()
        {
            string imagespath = System.Configuration.ConfigurationManager.AppSettings["imagespath"];
            ViewData["imagespath"] = imagespath;
            return View();
        }
        [AuthorizeFilter(ActionName = "chart")]
        public ActionResult Chart() 
        {
            return View();
        }
        [AuthorizeFilter(ActionName = "diction")]
        public ActionResult Diction() 
        {
            return View();
        }

        [AuthorizeFilter(ActionName = "organization")]
        public ActionResult Organization() 
        {
            return View();
        }
        #endregion



        #region User

        [HttpPost]
        [AuthorizeFilter(ActionName = "user")]
        public JsonResult GetUserList(int limit = 10, int offset = 1, string user_name = "", string mobile = "")
        {
            B_Users b_user = new B_Users();
            List<Order> order = new List<Order>() { Order.Desc("id") };

            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="user_name",value=user_name,searchType=Common.EnumBase.SearchType.Like},
                new SearchTemplate(){key="mobile",value=mobile,searchType=Common.EnumBase.SearchType.Like},
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
            };
            var list_user = b_user.GetList(st, order);

            var total = b_user.GetCount(st);

            return Json(new { total = total, rows = list_user }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "user")]
        public JsonResult AddUser(string txt_user_name,string txt_mobile,string txt_email,string txt_nick_name,string txt_password) 
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="user_name",value=txt_user_name,searchType=Common.EnumBase.SearchType.Eq},
            };
            var res = b_user.GetCount(st);
            if (res > 0) 
            {
                json.status = -1;
                json.msg = "用户名已存在!";
                json.pitchId = "txt_user_name";
                return Json(json);
            }
            st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="mobile",value=txt_mobile,searchType=Common.EnumBase.SearchType.Eq},
                };
            res = b_user.GetCount(st);
            if (res > 0)
            {
                json.status = -1;
                json.msg = "手机号已存在!";
                json.pitchId = "txt_mobile";
                return Json(json);
            }
            Domain.Users m_user = new Domain.Users();
            m_user.user_name = txt_user_name;
            m_user.mobile = txt_mobile;
            m_user.password =  Common.Encrypt.md5(txt_password);
            m_user.email = txt_email;
            m_user.nick_name = txt_nick_name;
            res = b_user.Save(m_user);
            if (res <= 0) 
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            json.msg = "添加成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "user")]
        public JsonResult GetUser(int id) 
        {
            DAO.BLL.B_Users b_user = new B_Users();
            var res = b_user.GetUser(id);
            return Json(res);
        }
        [HttpPost]
        [AuthorizeFilter(ActionName = "user")]
        public JsonResult EditUser(int id,string txt_user_name, string txt_mobile, string txt_email, string txt_nick_name, string txt_password) 
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            var m_user = b_user.GetUser(id);
            if (m_user.user_name != txt_user_name) 
            {
                List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="user_name",value=txt_user_name,searchType=Common.EnumBase.SearchType.Eq},
                };
                var res = b_user.GetCount(st);
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "用户名已存在!";
                    json.pitchId = "txt_user_name";
                    return Json(json);
                }
            }
            if (m_user.mobile != txt_mobile) 
            {
                List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="mobile",value=txt_mobile,searchType=Common.EnumBase.SearchType.Eq},
                };
                var res = b_user.GetCount(st);
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "手机号已存在!";
                    json.pitchId = "txt_mobile";
                    return Json(json);
                }
            }
            m_user.user_name = txt_user_name;
            m_user.mobile = txt_mobile;
            if (m_user.password != txt_password)
            {
                m_user.password = Common.Encrypt.md5(txt_password);
            }
            m_user.email = txt_email;
            m_user.nick_name = txt_nick_name;
            b_user.Update(m_user);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "user")]
        public JsonResult DelUser(string ids) 
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            foreach (var id in ids.Split(new char[]{','}))
            {
                b_user.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }


        #endregion

        #region Navigation
        [HttpPost]
        [AuthorizeFilter(ActionName = "navigation")]
        public JsonResult GetNavList(int limit = 10, int offset = 1)
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            B_Navigation b_nav = new B_Navigation();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };

            List<Domain.Navigation> list = new List<Domain.Navigation>();
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="parent_id",value=0,searchType=Common.EnumBase.SearchType.Eq},
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
            };
            var list_nav = b_nav.GetList(st,order);
            var list_nav_cout = b_nav.GetCount(st);
            foreach (var nav in list_nav)
            {
                list.Add(nav);
                st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="parent_id",value=nav.id,searchType=Common.EnumBase.SearchType.Eq}
                };
                //查询二级菜单
                var list_sub_nav = b_nav.GetList(st, order);
                foreach (var sub_nav in list_sub_nav)
                {
                    list.Add(sub_nav);
                }
            }
            return Json(new { total = list_nav_cout, rows = list }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "navigation")]
        public JsonResult GetNavSubList(string index, string parent_id,int limit =10, int offset = 1) 
        {
            B_Navigation b_nav = new B_Navigation();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="parent_id",value=Convert.ToInt32(parent_id),searchType=Common.EnumBase.SearchType.Eq},
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
            };
            var list_nav = b_nav.GetList(st,order);
            return Json(list_nav, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [ValidateInput(false)]
        [AuthorizeFilter(ActionName = "navigation")]
        public JsonResult AddNav(string txt_parent_id, string txt_icon_url, string txt_title, string txt_link_url, string txt_sort_id, string txt_is_lock, string txt_action_type) 
        {
            Common.Json json = new Common.Json();
            B_Navigation b_nav = new B_Navigation();
            Domain.Navigation model = new Domain.Navigation();

            model.icon_url = txt_icon_url;
            model.title = txt_title;
            model.link_url = txt_link_url;
            model.sort_id = Convert.ToInt32(txt_sort_id);
            model.is_lock = txt_is_lock;
            model.parent_id = Convert.ToInt32(txt_parent_id);
            model.action_type = txt_action_type;
            if (model.parent_id == 0)
            {
                model.channel_id = 1;
            }
            else 
            {
                model.channel_id = 2;
            }
            var res = b_nav.Save(model);
            if (res <= 0)
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            json.msg = "添加成功!";

            return Json(json);
           
        }

        [HttpPost]
        [ValidateInput(false)]
        [AuthorizeFilter(ActionName = "navigation")]
        public JsonResult EditNav(int id, string txt_parent_id, string txt_icon_url, string txt_title, string txt_link_url, string txt_sort_id, string txt_is_lock, string txt_action_type)
        {
            Common.Json json = new Common.Json();
            B_Navigation b_nav = new B_Navigation();
            Domain.Navigation model = b_nav.GetNav(id);
            model.icon_url = txt_icon_url;
            model.title = txt_title;
            model.link_url = txt_link_url;
            model.sort_id = Convert.ToInt32(txt_sort_id);
            model.is_lock = txt_is_lock;
            model.parent_id = Convert.ToInt32(txt_parent_id);
            model.action_type = txt_action_type;
            if (model.parent_id == 0)
            {
                model.channel_id = 1;
            }
            else
            {
                model.channel_id = 2;
            }
            b_nav.Update(model);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "navigation")]
        public JsonResult GetNav(int id) 
        {
            B_Navigation b_nav = new B_Navigation();
            var model = b_nav.GetNav(id);
            return Json(model);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "navigation")]
        public JsonResult DelNav(string ids)
        {
            Common.Json json = new Common.Json();
            B_Navigation b_nav = new B_Navigation();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_nav.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region Manager_role
        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_role")]
        public JsonResult GetManager_roleList(int limit = 10, int offset = 1)
        {
            B_Manager_role b_mr = new B_Manager_role();
            List<Order> order = new List<Order>() { Order.Desc("id") };
            var list_user = b_mr.GetList(null, order);

            var total = b_mr.GetCount(null);

            return Json(new { total = total, rows = list_user }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_role")]
        public JsonResult AddManager_role(string txt_role_name, string txt_action_type)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Manager_role b_mr = new DAO.BLL.B_Manager_role();
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="role_name",value=txt_role_name,searchType = Common.EnumBase.SearchType.Eq}
            };
            var res = b_mr.GetCount(st);
            if (res > 0)
            {
                json.status = -1;
                json.msg = "角色名已存在!";
                json.pitchId = "txt_role_name";
                return Json(json);
            }
            Domain.Manager_role model = new Domain.Manager_role();
            model.role_name = txt_role_name;
            res = b_mr.Save(model);
            if (res <= 0)
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            B_Manager_role_value b_mrv = new B_Manager_role_value();
            b_mrv.Update(txt_action_type, res);
                json.msg = "添加成功!";
            
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_role")]
        public JsonResult CheckManager_role_value(int role_id,int nav_id,string action_type) 
        {
            B_Manager_role_value b_mrv = new B_Manager_role_value();
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="role_id",value=role_id,searchType=Common.EnumBase.SearchType.Eq},
                new SearchTemplate(){key="nav_id",value=nav_id,searchType=Common.EnumBase.SearchType.Eq}
            };
            var list = b_mrv.GetList(st,null);
            if (list.Count == 0)
            {
                return Json("false");
            }
            else 
            {
                if (list[0].action_type.Contains(action_type))
                {
                    return Json("true");
                }
                else 
                {
                    return Json("false");
                }
            }
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_role")]
        public JsonResult GetManager_role(int id) 
        {
            B_Manager_role b_mr = new B_Manager_role();
            var res = b_mr.Get(id);
            return Json(res);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_role")]
        public JsonResult EditManager_role(int id,string txt_role_name, string txt_action_type)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Manager_role b_mr = new DAO.BLL.B_Manager_role();
            var m_mr = b_mr.Get(id);
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="role_name",value=txt_role_name,searchType = Common.EnumBase.SearchType.Eq}
            };
            var res = b_mr.GetCount(st);
            if (m_mr.role_name != txt_role_name)
            {
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "角色名已存在!";
                    json.pitchId = "txt_role_name";
                    return Json(json);
                }
            }
            Domain.Manager_role model = m_mr;
            model.role_name = txt_role_name;
            b_mr.Update(model);
            B_Manager_role_value b_mrv = new B_Manager_role_value();
            b_mrv.Update(txt_action_type, model.id);
            json.msg = "修改成功!";

            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_role")]
        public JsonResult DelManager_role(string ids)
        {
            Common.Json json = new Common.Json();
            B_Manager_role b_mr = new B_Manager_role();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_mr.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }

        #endregion

        #region Manager

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager")]
        public JsonResult GetManagerList(int limit = 10, int offset = 1, string user_name = "", string mobile = "")
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            B_Manager b_manager = new B_Manager();
            List<Order> order = new List<Order>() { Order.Desc("id") };
            List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="user_name",value=user_name,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="mobile",value=mobile,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
                };
            var list_manager = b_manager.GetList(st,null);
            var total = b_manager.GetCount(st);
            return this.MyJson(new { total = total, rows = list_manager }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager")]
        public JsonResult AddManager(string txt_user_name, string txt_role_id, string txt_real_name, string txt_mobile, string txt_email, string txt_password, string txt_is_lock)
        {
            Common.Json json = new Common.Json();
            B_Manager b_manager = new B_Manager();
            List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="user_name",value=txt_user_name,searchType=Common.EnumBase.SearchType.Eq}
                };
            var res = b_manager.GetCount(st);
            if (res > 0)
            {
                json.status = -1;
                json.msg = "用户名已存在!";
                json.pitchId = "txt_user_name";
                return Json(json);
            }
            st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="mobile",value=txt_mobile,searchType=Common.EnumBase.SearchType.Eq}
                };
            res = b_manager.GetCount(st);
            if (res > 0)
            {
                json.status = -1;
                json.msg = "手机号已存在!";
                json.pitchId = "txt_mobile";
                return Json(json);
            }
            Domain.Manager model = new Domain.Manager();
            model.user_name = txt_user_name;
            model.real_name = txt_real_name;
            model.mobile = txt_mobile;
            model.email = txt_email;
            model.password = Common.Encrypt.md5(txt_password);
            model.is_lock = txt_is_lock;
            model.add_time = DateTime.Now;
            B_Manager_role b_mr = new B_Manager_role();
            //必须给一个角色对象,这里相当于又执行了一条update,可以在这里修改对应角色的内容(这就是NHibernate搞的鬼)
            model.manager_role = b_mr.Get(Convert.ToInt32(txt_role_id));
            res = b_manager.Save(model);
            if (res <= 0)
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            json.msg = "添加成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager")]
        public JsonResult GetManager(int id)
        {
            DAO.BLL.B_Manager b_manager = new B_Manager();
            var res = b_manager.Get(id);
            return Json(res);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager")]
        public JsonResult EditManager(string id, string txt_user_name, string txt_role_id, string txt_real_name, string txt_mobile, string txt_email, string txt_password, string txt_is_lock)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_Manager b_manager = new DAO.BLL.B_Manager();
            var m_manager = b_manager.Get(Convert.ToInt32(id));
            if (m_manager.user_name != txt_user_name)
            {
                List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="user_name",value=txt_user_name,searchType=Common.EnumBase.SearchType.Eq}
                };
                var res = b_manager.GetCount(st);
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "用户名已存在!";
                    json.pitchId = "txt_user_name";
                    return Json(json);
                }
            }
            if (m_manager.mobile != txt_mobile)
            {
                List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="mobile",value=txt_mobile,searchType=Common.EnumBase.SearchType.Eq}
                };
                var res = b_manager.GetCount(st);
                if (res > 0)
                {
                    json.status = -1;
                    json.msg = "手机号已存在!";
                    json.pitchId = "txt_mobile";
                    return Json(json);
                }
            }
            m_manager.user_name = txt_user_name;
            m_manager.real_name = txt_real_name;
            m_manager.mobile = txt_mobile;
            m_manager.email = txt_email;
            if (m_manager.password != Common.Encrypt.md5(txt_password))
            {
                m_manager.password = Common.Encrypt.md5(txt_password);
            }
            m_manager.is_lock = txt_is_lock;
            m_manager.add_time = DateTime.Now;
            B_Manager_role b_mr = new B_Manager_role();
            //cascade：有all、save-update、delete、none几个选项，表示  该表做一些操作时 是否作用于 关联的表，比如在一对多关系中如果cascade="all"时，那么父表所做的操作都会作用于子表，比如删除某个用户，那么这个用户下的文章也会关联一起删除。
            m_manager.manager_role = b_mr.Get(Convert.ToInt32(txt_role_id));
            b_manager.Update(m_manager);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "manager")]
        public JsonResult DelManager(string ids)
        {
            Common.Json json = new Common.Json();
            B_Manager b_manager = new B_Manager();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_manager.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region Manager_log


        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_log")]
        public JsonResult GetManager_logList(int limit = 10, int offset = 1, string user_name = "", DateTime? start_time = null, DateTime? end_time = null)
        {
            B_Manager_log b_log = new B_Manager_log();
            List<Order> order = new List<Order>() { Order.Desc("id") };
            List<SearchTemplate> st = new List<SearchTemplate>()
                {
                    new SearchTemplate(){key="user_name",value=user_name,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="start_time",value=start_time,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="end_time",value=end_time,searchType=Common.EnumBase.SearchType.Eq},
                    new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
                };
            var list_manager = b_log.GetList(st,order);
            var total = b_log.GetCount(st);
            return this.MyJson(new { total = total, rows = list_manager }, JsonRequestBehavior.AllowGet);
        }

     
        [HttpPost]
        [AuthorizeFilter(ActionName = "manager_log")]
        public JsonResult DelManager_log(string ids)
        {
            Common.Json json = new Common.Json();
            B_Manager_log b_log = new B_Manager_log();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_log.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region Send_sms

        [AuthorizeFilter(ActionName = "send_sms")]
        public JsonResult AddSend_sms(string appkey, string secret, string SmsFreeSignName, string SmsParam, string RecNum, string SmsTemplateCode) 
        {
            Sms sms = new Sms();
            var res = sms.Send(appkey, secret, SmsFreeSignName, SmsParam, RecNum, SmsTemplateCode);
            Common.Json json = new Common.Json();
            if (res == null || !res.Success) 
            {
                json.status = -1;
                json.msg = "短信发送失败!";
            }
            else 
            {
                json.msg = "短信发送成功!";
            }
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Backups


        [HttpPost]
        [AuthorizeFilter(ActionName = "backups")]
        public JsonResult GetBackupsList(int limit = 10, int offset = 1, string fileName = "", DateTime? start_time = null, DateTime? end_time = null)
        {
            B_Backups b_backup = new B_Backups();
            List<Order> order = new List<Order>() { Order.Desc("id") };
            List<SearchTemplate> st = new List<SearchTemplate>() 
            {
                new SearchTemplate(){key="fileName",value=fileName,searchType=EnumBase.SearchType.Eq},
                new SearchTemplate(){key="start_time",value=fileName,searchType=EnumBase.SearchType.Ge},
                new SearchTemplate(){key="end_time",value=fileName,searchType=EnumBase.SearchType.Le},
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=EnumBase.SearchType.Paging}
            };
            var list_manager = b_backup.GetList(st,order);
            var total = b_backup.GetCount(st);
            return this.MyJson(new { total = total, rows = list_manager }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "backups")]
        public JsonResult AddBackups(string txt_dbName, string txt_fileName, string txt_backupType, string txt_remark)
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            Common.Json json = new Common.Json();
            B_Backups b_backup = new B_Backups();
            B_Manager b_manager = new B_Manager();
            txt_fileName += ".bak";
            var filePath = Server.MapPath("~/Resource/DbBackup/" + txt_fileName);
            //把文件备份成功
            b_backup.BackupDB(txt_dbName, filePath, Convert.ToInt32(txt_backupType));
            Domain.Backups m_backup = new Domain.Backups();
            m_backup.dbName = txt_dbName;
            m_backup.fileName = txt_fileName;
            m_backup.filePath = "/Resource/DbBackup/" + txt_fileName;
            if(Convert.ToInt32(txt_backupType) == Convert.ToInt32(EnumBase.BackupType.完整备份)){
                m_backup.backupType = EnumBase.BackupType.完整备份.Description();
            }
            else if (Convert.ToInt32(txt_backupType) == Convert.ToInt32(EnumBase.BackupType.差异备份))
            {
                m_backup.backupType = EnumBase.BackupType.差异备份.Description();
            }
            else
            {
                m_backup.backupType = EnumBase.BackupType.完整备份.Description();
            }
            m_backup.fileSize = FileHelper.ToFileSize(FileHelper.GetFileSize(filePath));
            m_backup.addTime = DateTime.Now;
            m_backup.addManager = b_manager.Get(Convert.ToInt32(base.User.Identity.Name));
            m_backup.remark = txt_remark;
            m_backup.delManager = new Domain.Manager();//这里给个空对象,否则外键关联会出错的
            var res = b_backup.Save(m_backup);
            if (res > 0) 
            {
                json.msg = "备份成功!";
            }
            else 
            {
                json.msg = "备份失败!";
                json.status = -1;
            }
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "backups")]
        public JsonResult DelBackups(string ids)
        {
            Common.Json json = new Common.Json();
            B_Backups b_backup = new B_Backups();
            B_Manager b_mananger = new B_Manager();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                var model = b_backup.Get(Convert.ToInt32(id));
                string filepath = Server.MapPath(model.filePath);
                if (FileDownHelper.FileExists(filepath))
                {
                    FileHelper.DeleteFile(filepath);
                }
                model.delManager = b_mananger.Get(Convert.ToInt32(base.User.Identity.Name));
                model.delTime = DateTime.Now;
                b_backup.Update(model);
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "个备份!";
            return Json(json);
        }


        [HttpPost]
        [AuthorizeFilter(ActionName = "backups")]
        public void DownLoadBackups(string id)
        {
            B_Backups b_backup = new B_Backups();

            var data = b_backup.Get(Convert.ToInt32(id));
            string filename = Server.UrlDecode(data.fileName);
            string filepath = Server.MapPath(data.filePath);
            if (FileDownHelper.FileExists(filepath))
            {
                FileDownHelper.DownLoadold(filepath, filename);
            }
        }
        #endregion

        #region News_type
        [HttpPost]
        [AuthorizeFilter(ActionName = "news_type")]
        public JsonResult GetNews_typeList(int limit = 10, int offset = 1)
        {
            B_News_type b_nt = new B_News_type();
            List<Order> order = new List<Order>() { Order.Asc("id") };
            List<SearchTemplate> st = new List<SearchTemplate>() 
            {
                new SearchTemplate(){key="parent_id",value=0,searchType = EnumBase.SearchType.Eq},
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
            };
            List<Domain.News_type> list = new List<Domain.News_type>();
            var list_nt = b_nt.GetList(st, order);
            var list_nt_count = b_nt.GetCount(st);
            foreach (var item in list_nt)
            {
                list.Add(item);
                st = new List<SearchTemplate>() 
                {
                    new SearchTemplate(){key="parent_id",value=item.id,searchType = EnumBase.SearchType.Eq}
                };
                var list_ntsub = b_nt.GetList(st, order);
                foreach (var sub in list_ntsub)
                {
                    list.Add(sub);
                }

            }
            var total = list_nt_count;

            return Json(new { total = total, rows = list }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "news_type")]
        public JsonResult AddNews_type(int txt_parent_id, string txt_title, int? txt_sort_id, string txt_seo_title, string txt_seo_keywords, string txt_seo_description)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_News_type b_nt = new DAO.BLL.B_News_type();
            Domain.News_type m_nt = new Domain.News_type();
            m_nt.parent_id =  txt_parent_id;
            m_nt.title = txt_title;
            m_nt.sort_id = txt_sort_id;
            m_nt.seo_title = txt_seo_title;
            m_nt.seo_keywords = txt_seo_keywords;
            m_nt.seo_description = txt_seo_description;
            var res = b_nt.Save(m_nt);
            if (res > 0)
            {
                json.msg = "添加成功!";
            }
            else
            {
                json.msg = "添加失败!";
                json.status = -1;
            }

            return Json(json);
        }


        [HttpPost]
        [AuthorizeFilter(ActionName = "news_type")]
        public JsonResult GetNews_type(int id)
        {
            B_News_type b_nt = new B_News_type();
            var res = b_nt.Get(id);
            return Json(res);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "news_type")]
        public JsonResult EditNews_type(int id,int txt_parent_id, string txt_title, int? txt_sort_id, string txt_seo_title, string txt_seo_keywords, string txt_seo_description)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_News_type b_nt = new DAO.BLL.B_News_type();
            var m_nt = b_nt.Get(id);
            m_nt.parent_id = txt_parent_id;
            m_nt.title = txt_title;
            m_nt.sort_id = txt_sort_id;
            m_nt.seo_title = txt_seo_title;
            m_nt.seo_keywords = txt_seo_keywords;
            m_nt.seo_description = txt_seo_description;
            b_nt.Update(m_nt);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "news_type")]
        public JsonResult DelNews_type(string ids)
        {
            Common.Json json = new Common.Json();
            B_News_type b_nt = new B_News_type();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_nt.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region News
        [HttpPost]
        [AuthorizeFilter(ActionName = "news")]
        public JsonResult GetNewsList(int limit = 10, int offset = 1)
        {
            B_News b_new = new B_News();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };
            List<SearchTemplate> st = new List<SearchTemplate>() 
            {
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=EnumBase.SearchType.Paging}
            };
            var list_new = b_new.GetList(st, order);
            var list_new_count = b_new.GetCount(st);
            return Json(new { total = list_new_count, rows = list_new }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        [AuthorizeFilter(ActionName = "news")]
        public JsonResult AddNews(int txt_parent_id, string txt_is_lock, string txt_action_type, string txt_title, int? txt_sort_id, int? txt_click, DateTime? start_time, string txt_source, string txt_author, string txt_summary, string txtContent, string txt_seo_title, string txt_seo_keywords, string txt_seo_description)
        {
            Common.Json json = new Common.Json();
            var fileName = "";
            var name = "";
            if (Request.Files.Count == 0) 
            { 
                //json.msg = "没有文件！";
                //json.status = -1;
                //return Json(json);
            }
            else
            {
                HttpPostedFileBase file = Request.Files[0];
                name = "news/" + DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(file.FileName);
                fileName = Path.Combine(Request.MapPath("/") + "../Images", name);
                try
                {
                    file.SaveAs(fileName);
                }
                catch
                {
                    //json.msg = "上传异常！";
                    //json.status = -1;
                    //return Json(json);
                }
            }

            DAO.BLL.B_News b_new = new DAO.BLL.B_News();
            DAO.BLL.B_News_type b_nt = new B_News_type();
            DAO.BLL.B_Manager b_manager = new B_Manager();
            Domain.News m_new = new Domain.News();
            m_new.news_type = b_nt.Get(txt_parent_id);
            m_new.is_lock = txt_is_lock;
            if (!string.IsNullOrEmpty(txt_action_type)) 
            {
                if (txt_action_type.Contains(EnumBase.RecommendType.置顶.Description())) 
                {
                    m_new.is_top = 1;
                }
                if (txt_action_type.Contains(EnumBase.RecommendType.热门.Description()))
                {
                    m_new.is_hot = 1;
                }
                if (txt_action_type.Contains(EnumBase.RecommendType.推荐.Description()))
                {
                    m_new.is_red = 1;
                }
                if (txt_action_type.Contains(EnumBase.RecommendType.允许评论.Description()))
                {
                    m_new.is_msg = 1;
                }
            }
            m_new.title = txt_title;
            m_new.img_url = name;
            m_new.sort_id = txt_sort_id;
            m_new.source = txt_source;
            m_new.author = txt_author;
            m_new.summary = txt_summary;
            m_new.content = txtContent;
            m_new.start_time = start_time;
            m_new.add_time = DateTime.Now;
            m_new.click = txt_click;
            m_new.manager = b_manager.Get(Convert.ToInt32(base.User.Identity.Name));
            m_new.seo_title = txt_seo_title;
            m_new.seo_keywords = txt_seo_keywords;
            m_new.seo_description = txt_seo_description;
            var res = b_new.Save(m_new);
            if (res > 0)
            {
                json.msg = "添加成功!";
            }
            else
            {
                json.msg = "添加失败!";
                json.status = -1;
            }
            return Json(json);
        }


        [HttpPost]
        [AuthorizeFilter(ActionName = "news")]
        public JsonResult GetNews(int id)
        {
            B_News b_new = new B_News();
            var res = b_new.Get(id);
            return Json(res);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "news")]
        public JsonResult EditNews(int id, int txt_parent_id, string txt_is_lock, string txt_action_type, string txt_title, int? txt_sort_id, int? txt_click, DateTime? start_time, string txt_source, string txt_author, string txt_summary, string txtContent, string txt_seo_title, string txt_seo_keywords, string txt_seo_description,string txt_img_url)
        {
            Common.Json json = new Common.Json();
            DAO.BLL.B_News b_new = new DAO.BLL.B_News();
            DAO.BLL.B_News_type b_nt = new B_News_type();
            DAO.BLL.B_Manager b_manager = new B_Manager();
            Domain.News m_new = b_new.Get(id);
            string pastImg = "";
            if (txt_img_url != m_new.img_url)//如果原图不等于现在的图片
            {
                var fileName = "";
                var name = "";
                if (Request.Files.Count == 0)
                {
                    //json.msg = "没有文件！";
                    //json.status = -1;
                    //return Json(json);
                }
                else
                {
                    pastImg = Path.Combine(Request.MapPath("/") + "../Images", m_new.img_url);//旧文件的物理路径
                    HttpPostedFileBase file = Request.Files[0];
                    name = "news/" + DateTime.Now.ToString("yyyyMMddhhmmss") + Path.GetExtension(file.FileName);
                    fileName = Path.Combine(Request.MapPath("/") + "../Images", name);
                    try
                    {
                        file.SaveAs(fileName);
                        m_new.img_url = name;
                    }
                    catch
                    {
                        //json.msg = "上传异常！";
                        //json.status = -1;
                        //return Json(json);
                    }
                }
            }
            m_new.news_type = b_nt.Get(txt_parent_id);
            m_new.is_lock = txt_is_lock;
            if (!string.IsNullOrEmpty(txt_action_type))
            {
                m_new.is_top = 0;
                m_new.is_hot = 0;
                m_new.is_red = 0;
                m_new.is_msg = 0;
                if (txt_action_type.Contains(EnumBase.RecommendType.置顶.Description()))
                {
                    m_new.is_top = 1;
                }
                if (txt_action_type.Contains(EnumBase.RecommendType.热门.Description()))
                {
                    m_new.is_hot = 1;
                }
                if (txt_action_type.Contains(EnumBase.RecommendType.推荐.Description()))
                {
                    m_new.is_red = 1;
                }
                if (txt_action_type.Contains(EnumBase.RecommendType.允许评论.Description()))
                {
                    m_new.is_msg = 1;
                }
            }
            m_new.title = txt_title;
            m_new.sort_id = txt_sort_id;
            m_new.source = txt_source;
            m_new.author = txt_author;
            m_new.summary = txt_summary;
            m_new.content = txtContent;
            m_new.start_time = start_time;
            m_new.update_time = DateTime.Now;
            m_new.click = txt_click;
            m_new.manager = b_manager.Get(Convert.ToInt32(base.User.Identity.Name));
            m_new.seo_title = txt_seo_title;
            m_new.seo_keywords = txt_seo_keywords;
            m_new.seo_description = txt_seo_description;
            b_new.Update(m_new);
            json.msg = "修改成功!";
            Common.FileHelper.DeleteFile(pastImg);
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "news")]
        public JsonResult DelNews(string ids)
        {
            Common.Json json = new Common.Json();
            B_News b_new = new B_News();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                var m_new = b_new.Get(Convert.ToInt32(id));
                b_new.Delete(Convert.ToInt32(id));
                string pastImg = Path.Combine(Request.MapPath("/") + "../Images", m_new.img_url);//旧文件的物理路径
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }

        public JsonResult ExamNews(string ids,int? txt_status) 
        {
            Common.Json json = new Common.Json();
            B_News b_new = new B_News();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                var m_new = b_new.Get(Convert.ToInt32(id));
                if (m_new.status != txt_status)
                {
                    m_new.status = txt_status;
                    b_new.Update(m_new);
                }
            }
            json.msg = "成功审核" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region Chart
        [AuthorizeFilter(ActionName = "chart")]
        [HttpPost]
        public JsonResult GetChart()
        {

            B_Manager_log b_log = new B_Manager_log();
            var res = b_log.GetNavClick(7);
            List<string> legend = new List<string>();
            List<Series> list = new List<Series>();
            foreach (var item in res.Keys)
            {
                legend.Add(item);
                Series menuSeries = new Series();
                menuSeries.name = item;
                menuSeries.type = "line";
                menuSeries.areaStyle = new itemStyle() { normal = new object() };
                menuSeries.data = res[item].ToList();
                //menuSeries.stack = "总量";
                list.Add(menuSeries);
            }

            xAxis xaxis = new xAxis()
            {
                boundaryGap = false,
                type = "category",
                data = new List<object>() { DateTime.Now.AddDays(-6).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-4).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd"), DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd") }
            };


            List<object> data = new List<object>();
            data.Add(legend);
            data.Add(xaxis);
            data.Add(list);

            return Json(data);
        }
        #endregion


        #region Organization
        [HttpPost]
        [AuthorizeFilter(ActionName = "organization")]
        public JsonResult GetOrganizationList(int limit = 10, int offset = 1)
        {
            B_Organization b_org = new B_Organization();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };

            List<Domain.Navigation> list = new List<Domain.Navigation>();
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="parent_id",value=0,searchType=Common.EnumBase.SearchType.Eq},
                new SearchTemplate(){key="",value=new int[]{offset,limit},searchType=Common.EnumBase.SearchType.Paging}
            };
            var list_org = b_org.GetList(st, order);
            var list_org_cout = b_org.GetCount(st);
            return Json(new { total = list_org_cout, rows = list_org }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "organization")]
        public JsonResult GetOrganizationSubList(string parent_id)
        {
            B_Organization b_org = new B_Organization();
            List<Order> order = new List<Order>() { Order.Asc("sort_id") };
            List<SearchTemplate> st = new List<SearchTemplate>()
            {
                new SearchTemplate(){key="parent_id",value=Convert.ToInt32(parent_id),searchType=Common.EnumBase.SearchType.Eq}
            };
            var list_sub_org = b_org.GetList(st, order);
            return Json(list_sub_org, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [ValidateInput(false)]
        [AuthorizeFilter(ActionName = "organization")]
        public JsonResult AddOrganization(FormCollection form)
        {
            Common.Json json = new Common.Json();
            B_Organization b_org = new B_Organization();
            B_Organization_type b_orgtype = new B_Organization_type();
            B_Manager b_manager = new B_Manager();
            Domain.Organization model = new Domain.Organization();

            model.name = form["txt_name"];
            model.parent_id = Convert.ToInt32(form["txt_parent_id"]);
            if (model.parent_id != 0)
            {
                var m = b_org.Get(model.parent_id);
                if (string.IsNullOrEmpty(model.parent_ids)) 
                {
                    model.parent_ids = model.parent_id + ",";
                }
                else
                {
                    model.parent_ids = m.parent_ids + m.parent_id + ",";
                }
                model.levels = m.levels + 1;
            }
            else 
            {
                model.levels = 0;
            }
            model.orgtype = b_orgtype.Get(Convert.ToInt32(form["txt_orgtype_id"]));
            model.sort_id = Convert.ToInt32(form["txt_sort_id"]);
            model.add_manager = b_manager.Get(Convert.ToInt32(base.User.Identity.Name));
            model.add_time = DateTime.Now;
            model.status = Convert.ToInt32(form["txt_status"]);
            var res = b_org.Save(model);
            if (res <= 0)
            {
                json.status = -1;
                json.msg = "添加失败!";
                return Json(json);
            }
            json.msg = "添加成功!";

            return Json(json);

        }

        [HttpPost]
        [ValidateInput(false)]
        [AuthorizeFilter(ActionName = "organization")]
        public JsonResult Editorganization(FormCollection form)
        {
            Common.Json json = new Common.Json();
            B_Organization b_org = new B_Organization();
            B_Organization_type b_orgtype = new B_Organization_type();
            B_Manager b_manager = new B_Manager();
            Domain.Organization model = b_org.Get(Convert.ToInt32(form["id"]));

            model.name = form["txt_name"];
            model.parent_id = Convert.ToInt32(form["txt_parent_id"]);
            if (model.parent_id != 0)
            {
                var m = b_org.Get(model.parent_id);
                if (string.IsNullOrEmpty(model.parent_ids))
                {
                    model.parent_ids = model.parent_id + ",";
                }
                else
                {
                    model.parent_ids = m.parent_ids + m.parent_id + ",";
                }
                model.levels = m.levels + 1;
            }
            else
            {
                model.levels = 0;
            }
            model.orgtype = b_orgtype.Get(Convert.ToInt32(form["txt_orgtype_id"]));
            model.sort_id = Convert.ToInt32(form["txt_sort_id"]);
            model.status = Convert.ToInt32(form["txt_status"]);
            b_org.Update(model);
            json.msg = "修改成功!";
            return Json(json);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "organization")]
        public JsonResult GetOrganization(int id)
        {
            B_Organization b_org = new B_Organization();
            var model = b_org.Get(id);
            return Json(model);
        }

        [HttpPost]
        [AuthorizeFilter(ActionName = "organization")]
        public JsonResult DelOrganization(string ids)
        {
            Common.Json json = new Common.Json();
            B_Organization b_org = new B_Organization();
            foreach (var id in ids.Split(new char[] { ',' }))
            {
                b_org.Delete(Convert.ToInt32(id));
            }
            json.msg = "成功删除" + ids.Split(new char[] { ',' }).Length + "条记录!";
            return Json(json);
        }
        #endregion

        #region kindeditor富文本上传图片,预览图片

        [HttpPost]
        public JsonResult UploadImage()
        {
            string imagespath = System.Configuration.ConfigurationManager.AppSettings["imagespath"];
            string savePath = "/UploadImages/";
            string saveUrl = "/UploadImages/";
            string fileTypes = "gif,jpg,jpeg,png,bmp";
            int maxSize = 1000000;

            Hashtable hash = new Hashtable();

            HttpPostedFileBase file = Request.Files["imgFile"];
            if (file == null)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "请选择文件";
                return Json(hash, "text/html;charset=UTF-8");
            }

            string dirPath = Server.MapPath("/") + "../Images/" + savePath;
            if (!Directory.Exists(dirPath))
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传目录不存在";
                return Json(hash, "text/html;charset=UTF-8");
            }

            string fileName = file.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(','));

            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件大小超过限制";
                return Json(hash, "text/html;charset=UTF-8");
            }

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(fileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1)
            {
                hash = new Hashtable();
                hash["error"] = 1;
                hash["message"] = "上传文件扩展名是不允许的扩展名";
                return Json(hash, "text/html;charset=UTF-8");
            }

            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", System.Globalization.DateTimeFormatInfo.InvariantInfo) + fileExt;
            string filePath = dirPath + newFileName;
            file.SaveAs(filePath);
            string fileUrl = imagespath + saveUrl + newFileName;

            hash = new Hashtable();
            hash["error"] = 0;
            hash["url"] = fileUrl;

            return Json(hash, "text/html;charset=UTF-8");

        }


        //浏览方法
        [HttpGet]
        public ActionResult ProcessRequest()
        {
            string imagespath = System.Configuration.ConfigurationManager.AppSettings["imagespath"];
            //String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);

            //根目录路径，相对路径
            String rootPath = "/UploadImages/";  //站点目录+上传目录
            //根目录URL，可以指定绝对路径，
            String rootUrl =  "/UploadImages/";
            //图片显示的网络路径
            string networkUrl = imagespath + "UploadImages/";
            //图片扩展名
            String fileTypes = "gif,jpg,jpeg,png,bmp";

            String currentPath = "";
            String currentUrl = "";
            String currentDirPath = "";
            String moveupDirPath = "";

            //根据path参数，设置各路径和URL
            String path = Request.QueryString["path"];
            path = String.IsNullOrEmpty(path) ? "" : path;
            if (path == "")
            {
                currentPath = Server.MapPath("/") + "../Images/" + rootPath;
                currentUrl = Server.MapPath("/") + "../Images/" + rootUrl;
                currentDirPath = "";
                moveupDirPath = "";
            }
            else
            {
                currentPath = Server.MapPath(rootPath) + path;
                currentUrl = rootUrl + path;
                currentDirPath = path;
                moveupDirPath = Regex.Replace(currentDirPath, @"(.*?)[^\/]+\/$", "$1");
            }

            //排序形式，name or size or type
            String order = Request.QueryString["order"];
            order = String.IsNullOrEmpty(order) ? "" : order.ToLower();

            //不允许使用..移动到上一级目录
            if (Regex.IsMatch(path, @"\.\."))
            {
                Response.Write("Access is not allowed.");
                Response.End();
            }
            //最后一个字符不是/
            if (path != "" && !path.EndsWith("/"))
            {
                Response.Write("Parameter is not valid.");
                Response.End();
            }
            //目录不存在或不是目录
            if (!Directory.Exists(currentPath))
            {
                Response.Write("Directory does not exist.");
                Response.End();
            }

            //遍历目录取得文件信息
            string[] dirList = Directory.GetDirectories(currentPath);
            string[] fileList = Directory.GetFiles(currentPath);

            switch (order)
            {
                case "size":
                    Array.Sort(dirList, new NameSorter());
                    Array.Sort(fileList, new SizeSorter());
                    break;
                case "type":
                    Array.Sort(dirList, new NameSorter());
                    Array.Sort(fileList, new TypeSorter());
                    break;
                case "name":
                default:
                    Array.Sort(dirList, new NameSorter());
                    Array.Sort(fileList, new NameSorter());
                    break;
            }

            Hashtable result = new Hashtable();
            result["moveup_dir_path"] = moveupDirPath;
            result["current_dir_path"] = currentDirPath;
            //result["current_url"] = currentUrl;
            result["current_url"] = networkUrl;
            result["total_count"] = dirList.Length + fileList.Length;
            List<Hashtable> dirFileList = new List<Hashtable>();
            result["file_list"] = dirFileList;
            for (int i = 0; i < dirList.Length; i++)
            {
                DirectoryInfo dir = new DirectoryInfo(dirList[i]);
                Hashtable hash = new Hashtable();
                hash["is_dir"] = true;
                hash["has_file"] = (dir.GetFileSystemInfos().Length > 0);
                hash["filesize"] = 0;
                hash["is_photo"] = false;
                hash["filetype"] = "";
                hash["filename"] = dir.Name;
                hash["datetime"] = dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                dirFileList.Add(hash);
            }
            for (int i = 0; i < fileList.Length; i++)
            {
                FileInfo file = new FileInfo(fileList[i]);
                Hashtable hash = new Hashtable();
                hash["is_dir"] = false;
                hash["has_file"] = false;
                hash["filesize"] = file.Length;
                hash["is_photo"] = (Array.IndexOf(fileTypes.Split(','), file.Extension.Substring(1).ToLower()) >= 0);
                hash["filetype"] = file.Extension.Substring(1);
                hash["filename"] = file.Name;
                hash["datetime"] = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                dirFileList.Add(hash);
            }
            //Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
            //context.Response.Write(JsonMapper.ToJson(result));
            //context.Response.End();
            return Json(result, "text/html;charset=UTF-8", JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
